package cn.yunhe.control;

import cn.yunhe.entity.Vote;
import cn.yunhe.server.VoteServaer;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/VoteServlet")
public class VoteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String value = request.getParameter("vote");
        if (value!=null){
            int num = Integer.parseInt(value);
            String language = "";

            switch (num) {
                case 1:
                    language = "Java";
                    break;
                case 2:
                    language = "C";
                    break;
                case 3:
                    language = "Ruby";
                    break;
                case 4:
                    language="Pthyon";
                    break;
                case 5:
                    language = "PHP";
                    break;
            }
            VoteServaer voteServaer = new VoteServaer();
            Vote vote = new Vote(language);
            boolean flag = voteServaer.vote(vote);
            if (flag){
                request.setAttribute("result","投票成功!!");
                request.getRequestDispatcher("vote.jsp").forward(request,response);
            }else {
                request.setAttribute("result","投票失败,请重新投票!!");
                request.getRequestDispatcher("vote.jsp").forward(request,response);
            }
        }else {
            request.setAttribute("result","请选择语言!!");
            request.getRequestDispatcher("vote.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
